package com.seven.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.alibaba.fastjson.JSONArray;
import com.seven.entity.Users;
import com.seven.service.UsersService;

public class UsersServlet extends BaseServlet{
	
	UsersService usersService = new UsersService();
	
	/**
	 * 查询所有用户
	 * @throws IOException 
	 */
	public void selectAll(HttpServletRequest request,HttpServletResponse response) throws IOException {
		PrintWriter out =  response.getWriter();
		
		List<Users> list =  usersService.selectAllUsers();
		
		//放入json数组
		String jsonStr = JSONArray.toJSON(list).toString();
		
		out.print(jsonStr);
		
	}
	
	/**
	 * 管理员登陆
	 * @throws IOException 
	 * @throws ServletException 
	 */
	public void SysLogin(HttpServletRequest request,HttpServletResponse response) throws IOException, ServletException {
		String uname = request.getParameter("uname");
		String upwd = request.getParameter("upwd");
		
		if ("test".equals(uname)&&"test".equals(upwd)) {
			HttpSession session = request.getSession();
			session.setAttribute("username", "郑科");
			
			//重定向到主页
			response.sendRedirect("index.jsp");
		}else {
			//重定向到登陆界面
			request.getRequestDispatcher("login.jsp").forward(request, response);
		}
	}
	
	/**
	 * 退出登录
	 */
	public void logout(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		PrintWriter out = response.getWriter();
		HttpSession session = request.getSession();
		
		session.removeAttribute("username");
		
		out.print(1);
	}

}
